package me.green.ui.global.i18n;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 界面文本抽象实现
 *
 * @author green
 */
public class UITextFile implements UIText {

    private Map<String, String> dict;

    public UITextFile() {
        dict = new HashMap<String, String>();
        init("");
    }

    /**
     * 初始化界面文本字典
     *
     * @param language
     */
    void init(String language) {
        String file = "";
        if (null == language || "".equals(language)) {
            file = "i18n";
        } else {
            file = language;
        }
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = getClass().getResourceAsStream("/" + file + ".propertes");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            properties.load(bufferedReader);
            Enumeration<Object> keys = properties.keys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement().toString();
                dict.put(key, properties.getProperty(key));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }

    /**
     * 获取界面文本
     *
     * @param key
     * @return
     */
    @Override
    public String get(String key) {
        String s = dict.get(key);
        if (null == s) {
            s = "";
        }
        return s;
    }

}
